<?php

/**
*	头像上传
*
*/

//包含公共文件
include '../../common/common.php';

//接收传值
//var_dump($_FILES);
//数据键名,(图片name名)
$keys = array_keys($_FILES);
$key  = $keys[0];
// var_dump($key);

// 拼接保存文件的全路径
$path = DOC_ROOT . 'helper/upload/';
//var_dump($path);
$resPic   = upload("$key",$path);
//var_dump($resPic);
$picPath = $resPic[1];

//判断文件是否上传成功
if (!$resPic[0]) {
	exit("$resPic[1]");
}

//截取, 拼接数据库保存的路径
$fileArr     = explode('/' , $picPath);
$fileName = array_pop($fileArr);
$picture = 'helper/upload/' . $fileName;

// var_dump($fileArr); 
// var_dump($fileName); 
// var_dump($picture);

//将头像保存的文件路径更新到数据库中
$username = $_COOKIE['username'];
// var_dump($username);
$data  = ['picture' => "$picture"];
$resTx = myUpdate($link , DB_TABLE_USER , $data , "username = '$username'");
//var_dump($resTx);

//判断是否修改成功
if ($resTx && mysqli_affected_rows($link)) {

	//var_dump(mysqli_affected_rows($link));
	echo '文件路径保存成功';
} else {

	echo '文件路径保存失败';
}

//跳转到 上一页 ($_SERVER['HTTP_REFERER'])
header('refresh:1;url='  . $_SERVER['HTTP_REFERER']);
